package com.fhnew.app.ui.dialogactivity;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fhnew.app.R;
import com.fhnew.app.global.Constants;
import com.fhnew.app.global.Constants;
import com.fhnew.app.model.CustomObserver;
import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.model.network.HttpJsonUtils;
import com.fhnew.app.model.network.entity.req.BankName;
import com.fhnew.app.model.network.entity.req.update_PwdBean;
import com.fhnew.app.util.CheckView;
import com.fhnew.app.util.Md5;
import com.fhnew.app.util.SYSOUtils;

import org.angmarch.views.NiceSpinner;
import org.angmarch.views.OnSpinnerItemSelectedListener;

import java.util.LinkedList;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class BankActivity extends Activity {

    ImageView close;
    EditText bankcode;
    EditText rigistrealname;
    NiceSpinner spinner;
    EditText rigistidcode;
    EditText Withdrawal;
    EditText Withdrawal1;
    TextView Withdrawalbt;
    CheckView codeImg;
    TextView commitbt;
    TextView cancelbt;
    String[] code_reslut;
    String bankid;
    LinearLayout Withdrawallayout;
    LinearLayout bankbandlayout;
    LinkedList<String> data1 = new LinkedList<>();
    LinkedList<BankName.DataBean> data = new LinkedList<>();

    /**
     * 绑定银行卡弹框，先判断有没有设置取款密码，如果没有，先设置取款密码
     *
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Constants.backgroundvoic = false;
        setContentView(R.layout.activity_bank);
        getbanknamelist();
        close = findViewById(R.id.close);
        cancelbt = findViewById(R.id.cancelbt);
        Withdrawal = findViewById(R.id.Withdrawal);
        Withdrawal1 = findViewById(R.id.Withdrawal1);
        Withdrawalbt = findViewById(R.id.Withdrawalbt);
        bankbandlayout = findViewById(R.id.bankbandlayout);
        Withdrawallayout = findViewById(R.id.Withdrawallayout);
        bankcode = findViewById(R.id.bankcode);
        rigistrealname = findViewById(R.id.rigistrealname);
        spinner = findViewById(R.id.spinner);
        codeImg = findViewById(R.id.code_img);
        close = findViewById(R.id.close);
        commitbt = findViewById(R.id.commitbt);
        rigistidcode = findViewById(R.id.rigistidcode);
        if (UserModel.user.hasFundPwd) {
            //已绑定密码
            bankbandlayout.setVisibility(View.VISIBLE);
            Withdrawallayout.setVisibility(View.GONE);
        } else {
            bankbandlayout.setVisibility(View.GONE);
            Withdrawallayout.setVisibility(View.VISIBLE);
        }
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        spinner.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener() {
            @Override
            public void onItemSelected(NiceSpinner parent, View view, int position, long id) {
                bankid = data.get(position).bank_id + "";
            }
        });
        code_reslut = codeImg.getValidataAndSetImage();//生成验证码
        codeImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                code_reslut = codeImg.getValidataAndSetImage();//生成验证码
            }
        });


        commitbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String yanzheng = code_reslut[0] + code_reslut[1] + code_reslut[2] + code_reslut[3];
                if (TextUtils.isEmpty(bankcode.getText().toString().trim())) {
                    Toast.makeText(BankActivity.this, "请输入银行卡号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(rigistrealname.getText().toString().trim())) {
                    Toast.makeText(BankActivity.this, "请输入开户网点", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(rigistidcode.getText().toString().trim())) {
                    Toast.makeText(BankActivity.this, "请输入验证码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (rigistidcode.getText().toString().trim().equals(yanzheng)) {
                    bindBank();
                } else {
                    Toast.makeText(BankActivity.this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();
                }
            }
        });
        cancelbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Withdrawalbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(Withdrawal.getText().toString().trim())) {
                    Toast.makeText(BankActivity.this, "请输入登录密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(Withdrawal1.getText().toString().trim())) {
                    Toast.makeText(BankActivity.this, "请设置您的取款密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                saveFundPwd();
            }
        });
    }

    //设置密码
    private void saveFundPwd() {
        HttpJsonUtils.getHttpApiInstance()
                .saveFundPwd(Constants.getuseAdress() + "api/m/user/saveFundPwd", Md5.toMd5(Withdrawal.getText().toString().trim()),
                        Md5.toMd5(Withdrawal1.getText().toString().trim()), UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<update_PwdBean>() {
                    @Override
                    protected void onCustomNext(update_PwdBean resBanner) {
                        try {
                            if (resBanner.getCode() == 0) {
                                Withdrawal.setText("");
                                Withdrawal1.setText("");
                                Toast.makeText(BankActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                                UserModel.user.hasFundPwd = true;
                                Withdrawallayout.setVisibility(View.GONE);
                                bankbandlayout.setVisibility(View.VISIBLE);
                            } else {
                                Toast.makeText(BankActivity.this, resBanner.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {

                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }

    private void bindBank() {
        HttpJsonUtils.getHttpApiInstance()
                .bindBank(Constants.getuseAdress() + "api/m/user/bindBank", bankid, bankcode.getText().toString().trim(), rigistrealname.getText().toString().trim(), UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<BankName>() {
                    @Override
                    protected void onCustomNext(BankName bankName) {
                        if (bankName.code == 0) {
                            Toast.makeText(BankActivity.this, "绑定成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });

    }

    private void getbanknamelist() {
        HttpJsonUtils.getHttpApiInstance()
                .getBanks(Constants.getuseAdress() + "api/m/user/getBanks", UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<BankName>() {
                    @Override
                    protected void onCustomNext(BankName bankName) {
                        bankid = bankName.data.get(0).bank_id + "";
                        data.clear();
                        data.addAll(bankName.data);
                        data1.clear();
                        for (int i = 0; i < bankName.data.size(); i++) {
                            data1.add(bankName.data.get(i).name);
                        }
                        spinner.attachDataSource(data1);
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }
}
